<?php
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
    $page_1 = 1;
    $min = 0;
    $page_2 = $page + 1;
} else {
    if ($page > 1) {
        $page_2 = $page + 1;
        $min = ($page - 1) * 35;
        $page_1 = $page - 1;
    } else {
        $page_1 = 1;
        $min = 0;
        $page_2 = 2;
    }
}
$max = $page * 35;
$sql = "select d.* from `sharesite` d where d.`state` <> 3 order by d.`id` desc limit {$min},{$max}";
$update_id = guolv($_GET['update_id']);
$state = guolv($_GET['state']);
if (is_numeric($update_id) && is_numeric($state)) {
    $mysql->query("update `sharesite` d set d.`state` = {$state} where `id`='{$update_id}'");
    echo '<script>alert(\'操作成功\');location.href=\'sharesite.php\'</script>';
    exit;
}
if ($_GET['do'] == 'search') {
    $name = guolv($_GET['name']);
    if (substr($name, 0, 1) == '#') {
        $search_id = str_replace('#', '', $name);
        $sql = "select d.* from `sharesite` d where `id`='{$search_id}' order by `id` desc limit {$min},{$max}";
    } else {
        $sql = "select d.* from `sharesite` d where d.`name` like '%{$name}%' and d.`state` <> 3 order by `id` desc limit {$min},{$max}";
    }
}
$row_sharesite = $mysql->query($sql);
?>

<?php include('head.php') ?>

<div class="container-fluid">

    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span10">
            <h1 class="page-title">分享域名管理</h1>

            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well">
                            <form class="navbar-search pull-left" action="sharesite.php" method="get">
                                <input type="hidden" name="do" value="search">
                                <input type="text" class="search-query" placeholder="搜索域名+回车" name="name">
                            </form>
                            <div class="btn-group pull-right">
                                <a href="sharesite.php" class="btn">首页</a></button>
                                <a href="sharesite.php?page=<?php echo $page_1 ?>" class="btn"
                                   style="margin-left:5px;">上一页</a>
                                <a href="sharesite.php?page=<?php echo $page_2 ?>" class="btn"
                                   style="margin-left:5px;">下一页</a>
                            </div>
                            <table class="table">
                                <thead>
                                <tr>
                                    <th>域名</th>
                                    <th>类型</th>
                                    <th>备注</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php
                                foreach ($row_sharesite as $v_sharesite) {
                                    $s = $v_sharesite['state'];
                                    $btn = "";
                                    switch ($s) {
                                        case '0': {
                                            $s = '<p style="color:blue">未使用</p>';
                                            $btn = "
<a class=\"btn\" style=\"color:green\" onclick=\"update_id({$v_sharesite['id']}, 1)\" title=\"使用中\"><i class=\"icon-check\"></i></a>
<a class=\"btn\" style=\"color:red\" onclick=\"update_id({$v_sharesite['id']}, 2)\" title=\"微信屏蔽\"><i class=\"icon-remove\"></i></a>
<a class=\"btn\" style=\"color:darkgrey\" title=\"删除域名\" onclick=\"update_id({$v_sharesite['id']}, 3)\"><i class=\"icon-trash\"></i></a>";
                                            break;
                                        }
                                        case '1': {
                                            $s = '<p style="color:green">使用中</p>';
                                            $btn = "
<a class=\"btn\" style=\"color:blue\" onclick=\"update_id({$v_sharesite['id']}, 0)\" title=\"未使用\"><i class=\"icon-undo\"></i></a>
<a class=\"btn\" style=\"color:red\" onclick=\"update_id({$v_sharesite['id']}, 2)\" title=\"微信屏蔽\"><i class=\"icon-remove\"></i></a>
<a class=\"btn\" style=\"color:darkgrey\" title=\"删除域名\" onclick=\"update_id({$v_sharesite['id']}, 3)\"><i class=\"icon-trash\"></i></a>";
                                            break;
                                        }
                                        case '2': {
                                            $s = '<p style="color:red">微信屏蔽</p>';
                                            $btn = "
<a class=\"btn\" style=\"color:blue\" onclick=\"update_id({$v_sharesite['id']}, 0)\" title=\"未使用\"><i class=\"icon-undo\"></i></a>
<a class=\"btn\" style=\"color:green\" onclick=\"update_id({$v_sharesite['id']}, 1)\" title=\"使用中\"><i class=\"icon-check\"></i></a>
<a class=\"btn\" style=\"color:darkgrey\" title=\"删除域名\" onclick=\"update_id({$v_sharesite['id']}, 3)\"><i class=\"icon-trash\"></i></a>";
                                            break;
                                        }
                                    }
                                    print <<<table
        <tr>
     	  <td>{$v_sharesite['name']}</td>
		  <td>{$v_sharesite['type']}</td>
		  <td>{$v_sharesite['remark']}</td>
		  <td>{$s}</td>
		  <td>
		    <a class="btn btn-primary" href="edit_sharesite.php?id={$v_sharesite['id']}" title="编辑"><i class="icon-edit"></i></a>
		    {$btn}
		  </td>
        </tr>
table;
                                } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>


</div>

<footer>
    <hr>

    <p class="pull-right"><!--power by right--></p>


    <p><!--power by --></p>
</footer>


<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="lib/bootstrap/js/bootstrap.js"></script>
<script language="JavaScript">
    function update_id(id, state) {
        var str = "";
        switch (state) {
            case 0:
            {
                str = " [未使用] ";
                break;
            }
            case 1:
            {
                str = " [使用中] ";
                break;
            }
            case 2:
            {
                str = " [微信屏蔽] ";
                break;
            }
            case 3:
            {
                str = " [已删除] ";
                break;
            }
        }
        if (confirm("状态更新为" + str + "，确定操作？")) {
            location.href = "?update_id=" + id + "&state=" + state;
        } else {
            return false;
        }
    }
</script>
</body>
</html>